﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.ServiceModel;
using System.Windows;
using Microsoft.Practices.Prism.Regions;
using YunBox2.Client.Foundation.Events;
using YunBox2.Client.Infrastructure;
using YunBox2.Contracts;
using YunBox2.Contracts.Core;
using YunBox2.Contracts.FoundationModels;

using Microsoft.Practices.Prism.Events;

namespace YunBox2.Client.Foundation.ViewModels
{
	[Export]
	public class UOMEditorViewModel : DomainObject, IConfirmNavigationRequest
	{
	
		private UOM _dataSource;

		[Import]
		private IEventAggregator _eventAggregator;

		[ImportingConstructor]
		public UOMEditorViewModel()
        {
		    UOMTypeList = new ObservableCollection<KeyValuePair<Enum, string>>(EnumHelper.ToList(typeof (UOMType)));
		}

		public UOM DataSource
		{
			get
			{
				return _dataSource;
			}
			private set
			{
				if (_dataSource != value)
				{
					_dataSource = value;
					IsHasChanged = false;
					RaisePropertyChanged("DataSource");
				}
			}
		}

		public ObservableCollection<KeyValuePair<Enum, string>> UOMTypeList
		{
			get;
			private set;
		}

		public void Load(Guid id)
		{
			if (id == Guid.Empty)
			{
				Create();
				return;
			}
            using(var cs=ClientContext.Current.CreateOperationContextScope(ClientContext.Current.FoundationService as IContextChannel))
            {
                var uom = ClientContext.Current.FoundationService.GetUOMByID(id);
                Load(uom);
            }
		}

		public void Load(UOM uom)
		{
			if (uom == null)
			{
				throw new ArgumentNullException("uom");
			}
			uom.PropertyChanged += (s, e) => { IsHasChanged = true; };
			DataSource = uom;
		}

		public void Create()
		{
			Load(new UOM {IsReadOnly = false, Name = string.Empty, Rate = 1.0D, Symbol = string.Empty});
			IsNew = true;
		}

		public void Modify()
		{
			if (_dataSource == null)
			{
				return;
			}
			IsNew = false;
			IsReadOnly = false;
		}


		private void Remove()
		{
			if (_dataSource == null)
			{
				return;
			}
			if (!_dataSource.Id.HasValue)
			{
				Create();
				return;
			}


		}

		public void Save()
		{

		}

		

		public void CancelSave()
		{
			if (_dataSource == null)
			{
				return;
			}
			if (!_dataSource.Id.HasValue)
			{
				_dataSource = null;
				return;
			}
			Load(_dataSource.Id.Value);
		}

		#region States

		private bool _canSave;
		private bool _isHasChanged;
		private bool _isNew;
		private bool _isReadOnly = true;


		public bool IsReadOnly
		{
			get
			{
				if (_dataSource == null)
				{
					return true;
				}
				return !IsNew && _isReadOnly && _dataSource.IsReadOnly;
			}
			set
			{
				if (_isReadOnly != value)
				{
					_isReadOnly = value;
					RaisePropertyChanged("IsReadOnly");
				}
			}
		}

		public bool IsNew
		{
			get
			{
				if (_dataSource == null)
				{
					return false;
				}
				return (!_dataSource.Id.HasValue || _dataSource.Id.Value == Guid.Empty) && _isNew;
			}
			private set
			{
				if (_isNew != value)
				{
					_isNew = value;
					RaisePropertyChanged("IsCreating", "IsReadOnly");
				}
			}
		}

		public bool IsHasChanged
		{
			get
			{
				return IsNew || _isHasChanged;
			}
			private set
			{
				if (_isHasChanged != value)
				{
					_isHasChanged = value;
					RaisePropertyChanged("IsHasChanged");
				}
			}
		}

		public bool CanSave
		{
			get
			{
				if (_dataSource == null)
					return false;
				return !_dataSource.HasError && _canSave;
			}
			private set
			{
				if(_canSave!=value)
				{
					_canSave = value;
					RaisePropertyChanged("CanSave");
				}
			}
		}
		#endregion

		#region IConfirmNavigationRequest 成员

		private IRegionNavigationJournal navigationJournal;

		public bool IsNavigationTarget(NavigationContext navigationContext)
		{
			return true;
		}

		public void OnNavigatedFrom(NavigationContext navigationContext)
		{
			
		}

		public void OnNavigatedTo(NavigationContext navigationContext)
		{
			navigationJournal = navigationContext.NavigationService.Journal;
			Guid id;
			if (Guid.TryParse(navigationContext.Parameters["id"], out id))
			{
				Load(id);
			}
            else
			{
			    Create();
			}
			
		}


		public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
		{
            //if (IsHasChanged)
            //{
            //    MessageBoxResult result =Xceed.Wpf.Toolkit.MessageBox.Show("数据未保存,是否离开", "提示", MessageBoxButton.YesNo);
            //    continuationCallback(result == MessageBoxResult.Yes);
            //}
		    continuationCallback(true);
		}

		#endregion
	}
}